package com.example.iscore;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;

public class CountActivity extends ListActivity {

   //private Button nouveau;
   private Button btnJoin ;
   private Button btnDel ;
   private List<String> maListe = new ArrayList<String>();
   private ArrayAdapter<String> adapter ;
 
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_count);
      //nouveau = (Button)findViewById(R.id.button1);
      btnJoin = (Button)findViewById(R.id.button2);
	  btnDel = (Button) findViewById(R.id.delete_btn);
	  btnDel.setEnabled(false);
      
	  /* contenu de la liste a remplacer par un select allGroups where idClient = ?, par la bdd" */
      maListe.add("Groupe1");
      maListe.add("Groupe2");
      maListe.add("Groupe3");
      maListe.add("Groupe4");
      maListe.add("Groupe5");
      
      adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, maListe);
      
      OnClickListener listenerJoin = new OnClickListener() {
          @Override
          public void onClick(View v) {
                  EditText edit = (EditText) findViewById(R.id.txtItem);
                  maListe.add(edit.getText().toString());
                  edit.setText("");
                  adapter.notifyDataSetChanged();
          }
      };
      
      OnClickListener listenerDel = new OnClickListener() {
	    	@Override
	    	public void onClick(View v) {
	    		SparseBooleanArray checkedItemPositions = getListView().getCheckedItemPositions();
	    		int itemCount = getListView().getCount();
	    		
	    		for(int i=itemCount-1; i >= 0; i--) {
	    			if(checkedItemPositions.get(i)){	    				
	    				adapter.remove(maListe.get(i));
	    				maListe.remove(i);
	    			}
	    		}	
	    		checkedItemPositions.clear();
	    	    adapter.notifyDataSetChanged();
	    	    
	    	}
	    };
	    
	    getListView().setOnItemClickListener(new OnItemClickListener() {
	    	   @Override
	    	   public void onItemClick(AdapterView<?> parent, View view,
	    	     int position, long id) {
	    	    // When clicked, show a toast with the TextView text
	    	    parent.getItemAtPosition(position);
	    	    btnDel.setEnabled(true);
	    	   }
	    });
	    
	    getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
	    	@Override
	    	public boolean onItemLongClick(AdapterView<?> parent, View view, int position,
	    			long id) {
	    		Intent intent = new Intent(CountActivity.this, MemberActivity.class);
	    		startActivity(intent);
	    		return true;
	    	}
	     });
	
	    btnJoin.setOnClickListener(listenerJoin);
	    btnDel.setOnClickListener(listenerDel); 
  
      setListAdapter(adapter);
   }

   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }

}